// pages/custompool/custompool.js
const app = getApp()
var CONFIG = require("../../config/config.js");
Page({
  data: {
    list: [],
    code: 0,
    msg: '',
    pages: 0,
    page: 0,
    count: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getUnclaimedUserInit();
  },

  onShow:function(){
    this.getUnclaimedUserInit();
  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getUnclaimedUserInit();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数 
   */
  onReachBottom: function () {
    this.getUnclaimedUserMore(this.data.page);
  },
  // 用户点击右上角分享

  onShareAppMessage: function () {
    return {
      title: CONFIG.SHARETITLE,
      path: CONFIG.PATHLOGIN
    }
  },

  getUnclaimedUserInit: function () {
    wx.showLoading({
      title: '加载中~',
      mask: true
    })
    let that = this;
    wx.request({
      url: CONFIG.API_HOST + 'crm/public/getUnclaimedUser',
      header: {
        'ContentType': 'application/json'
      },
      success: function (res) {
        // console.log(res);
        wx.hideLoading();
          that.setData({
            code: res.data.code,
            msg: res.data.msg,
            pages: res.data.pages,
            page: res.data.page,
            list: res.data.list
          })
        
      },
      fail: function (res) {
        wx.hideLoading();
        wx.showModal({
          title: CONFIG.MODEL_TITLE,
          showCancel: false,
          content: CONFIG.REQUEST_FAIL,
          confirmText: '点击重试',
          success: function (res) {
            wx.navigateBack();
          }
        })
      }
    })
  },

  getUnclaimedUserMore: function ( page) {
    wx.showLoading({
      title: '加载中~',
      mask: true
    })
    let that = this;
    wx.request({
      url: CONFIG.API_HOST + 'crm/public/getUnclaimedUser',
      data: {
        page: page
      },
      header: {
        'ContentType': 'application/json'
      },
      success: function (res) {
        wx.hideLoading();
        if (res.data.code == 1) {
          var newslist = res.data.list;
          var lists = that.data.list.concat(newslist);

          that.setData({
            code: res.data.code,
            msg: res.data.msg,
            pages: res.data.pages,
            page: res.data.page,
            list: lists
          })
        } else {
          that.setData({
            code: res.data.code,
            msg: res.data.msg,
            pages: res.data.pages,
            page: res.data.page,
            list: that.data.list
          })
        }
      },
      fail: function (res) {
        wx.hideLoading();
        wx.showModal({
          title: CONFIG.MODEL_TITLE,
          showCancel: false,
          content: '网络请求失败',
          confirmText: '点击重试',
          success: function (res) {
            wx.navigateBack();
          }
        })
      }
    })
  },

  // 拨打电话
  phoneCall: function (e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone,
      success: function () {
      },
    })
  },


// 点击认领
  renling:function(e){
    wx.showLoading({
      title: '认领中~',
      mask:true
    })
    var params = {
      consultant_id:app.globalData.admin_id,
      user_id:e.currentTarget.dataset.user_id
    }
    let that = this;
    wx.request({
      url: CONFIG.API_HOST + 'crm/user/renling',
      data: params,
      method: 'POST',
      success: function (res) {
        console.log(res);
        wx.hideLoading();
        wx.showToast({
          title: res.data.msg,
        })
        setTimeout(function(){
          that.getUnclaimedUserInit();
        },500)
      },
      fail: function (res) {
        // console.log(res);
        wx.hideLoading();
        wx.showModal({
          title: CONFIG.MODEL_TITLE,
          showCancel: false,
          content: CONFIG.REQUEST_FAIL,
          confirmText: '点击重试',
          success: function (res) {
            wx.navigateBack();
          }
        })
      }
    })
  }
})